//<?php

if(!$_SESSION) session_start();

if(!isset($_SESSION['UserID']) || $_SESSION['UserID'] == "") header("Location:index.php");
// Connect to the database
////////////////////////////////////////////////////////

require_once("scripts/config.php");

// Load Functions
////////////////////////////////////////////////////////

require_once("scripts/functions.php");

// Load Active Projects list
////////////////////////////////////////////////////////

$sql = "SELECT * FROM tblprojects WHERE set_active = 1";

$active_projects = sqlLoad($sql, $db);

foreach ($active_projects as $apl){

    $active .= ", ".$apl['ProjectID'];

}

$active = substr($active, 2);


// Load User Tasks
////////////////////////////////////////////////////////

$sql = "SELECT * FROM tbltasks WHERE UserID = ".$_SESSION['UserID']." AND set_complete < 100 AND set_status != 9 AND ProjectID IN ($active) ORDER BY set_priority DESC, date_due ASC";

$open_tasks = sqlLoad($sql, $db);

$sql = "SELECT * FROM tbltasks WHERE UserID = 0 AND set_complete < 100 AND set_status != 9 AND ProjectID IN ($active) ORDER BY set_priority DESC, date_due ASC";

$unassigned_tasks = sqlLoad($sql, $db);

$sql = "SELECT * FROM tbltasks WHERE UserID != ".$_SESSION['UserID']." AND UserID != 0 AND set_complete < 100 AND set_status != 9 AND ProjectID IN ($active) ORDER BY set_priority DESC, date_due ASC";

$assigned_tasks = sqlLoad($sql, $db);

$sql = "SELECT * FROM tblusers";

$users = sqlLoad($sql, $db);

$sql = "SELECT COUNT(*) as Notes, TaskID FROM tbltask_notes GROUP BY TaskID";

$notes = sqlLoad($sql, $db);

$sql = "SELECT * FROM tblprojects";

$projects = sqlLoad($sql, $db);

$note_list = array();

foreach ($notes as $note){

$note_list[$note['TaskID']] = $note['Notes'];

}

$user_list = array();

foreach ($users as $user){

$user_list[$user['UserID']] =  $user['name_display'];

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home Task List | SynergyBugs | SynergyMX.com</title>
<script src="https://www.google.com/jsapi?key=<?php echo $api_key;?>" type="text/javascript"></script>
<script language="javascript" type="text/javascript">

    google.load("jquery", "1.4.4");
    google.load("jqueryui", "1.8.6");

</script>
<script language="javascript" type="text/javascript" src="scripts/jquery-forms.js"></script>
<script language="javascript" type="text/javascript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function newTask(){

		$('#dia_addtask_detail').load('forms/task_new.php');

		$("#dia_addtask").dialog({
			bgiframe: true,
			width: 550,
			modal: true,
			position: 'top',
			beforeclose: function() {$(this).dialog('destroy');},
			buttons: {
				'Save and Add Task': function() {

					$('#formVNT').ajaxSubmit({target:'#dia_addtask_detail'});

					location.reload();

					return false;

				},
				Cancel: function() {
					$(this).dialog('destroy');
				}
			}
		});

		$('#date_due').datepicker({ showButtonPanel: true });

	};


</script>
<link href="scripts/style.css" rel="stylesheet" type="text/css" />
<link href="scripts/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />

</head>
<body>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td class="tnbg"><table align="center" width="780" border="0" cellspacing="0" cellpadding="0">
                  <tr><td width="195"><a href="home.php"><img src="/images/logo.png" width="195" height="56" border="0"></a></td><td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td class="tnfck ft8b" height="16" align="right">Welcome Back, <?php echo $_SESSION['name_display'];?>&nbsp;&nbsp;<?php if($_SESSION['set_admin']==1) echo "::&nbsp;&nbsp;<a href='settings.php'>Settings</a>&nbsp;&nbsp;";?>::&nbsp;&nbsp;<a href="index.php?action=signout">Sign Out</a></td>
                    </tr>
                    <tr>
                      <td height="40" align="right"><form id="form1" name="form1" method="post" action="">
                      <a href="projects.php"> Project List</a>&nbsp;|&nbsp;
<?php if(sizeof($projects)>0){ ?>
                        <a href="javascript:;" onclick="newTask();">add task</a>&nbsp;|&nbsp;
                        <?php } ?>
                          <input name="Search" type="text" id="Search" value="Search" />

                          <select name="fbs" id="fbs">
                            <option value="all">All Status</option>
                          </select>

<input name="Refresh" type="submit" id="Refresh" value="Refresh" />

                      </form></td>
                    </tr>
                </table></td></tr></table></td>
              </tr>
              <tr>
                <td>
<table width="780" border="0" cellspacing="0" cellpadding="0" align="center">
<?php if(strpos($_SERVER['HTTP_USER_AGENT'],"MSIE")) {?>
  <tr>
    <td style="padding:10px; background-color:#FFEAEA; border:1px solid #990000;">Some parts of this web site may not work with your crappy browser. Consider updating to <a href="http://www.google.com/chrome" target="_blank">Google Chrome</a> or <a href="http://www.getfirefox.com" target="_blank">Firefox</a>.</td>
  </tr>
  <?php } ?>
<?php if(sizeof($projects)==0){ ?>
  <tr>
    <td style="padding:10px; background-color:#FFEAEA; border:1px solid #990000;">No projects are defined. You cannot add tasks without them. <a href="projects.php">Click here to manage projects.</a></td>
  </tr>
<?php } ?>
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr valign="bottom">
        <td height="36"><span class="ft12b">Your Open Tasks:</span></td>
        </tr>
      <?php if(sizeof($open_tasks)>0){?>
      <tr>
        <td><div id="ATL">
          <table width="100%" border="0" cellspacing="0" cellpadding="2">
            <?php foreach ($open_tasks as $task){

			$bg = "#ffffff";

			if($row_count % 2){

				$bg = "#f0f0f0";

			}

			$row_count++;

			$note_icon = "comments0.gif";

			if($note_list[$task['TaskID']]){

			if($note_list[$task['TaskID']]==1) $note_icon = "comments1.gif";

			if($note_list[$task['TaskID']]>1) $note_icon = "comments2.gif";

			}
		?>
            <tr bgcolor="<?php echo $bg;?>" onclick="window.location='task.php?ID=<?php echo $task['TaskID'];?>';" onmouseover="this.className='hilight'" onmouseout="this.className=''">
              <td width="16"><img src="images/priority<?php echo $task['set_priority'];?>.gif" /></td>
              <td width="16"><img src="images/status<?php echo $task['set_status'];?>.gif" /></td>
              <td width="16"><img src="images/<?php echo $note_icon;?>" /></td>
              <td align="right"><?php echo $task['set_complete'];?>%</td>
              <td nowrap="nowrap"><?php echo date("M jS, Y",strtotime($task['date_due']));?></td>
              <td width="100%" nowrap="nowrap"><div style="width:600px; overflow:hidden;"><?php echo $task['Subject'];?> <span style="color:#999"><?php echo str_chop(RemoveHTML($task['Body']),100);?></span></div></td>
              </tr>
            <?php } ?>
            </table>
          </div></td>
        </tr>
      <?php } else {?>
      <tr>
        <td style="padding:10px">No Tasks Found</td>
        </tr>
      <?php } ?>
      <tr valign="bottom">
        <td height="36"><div><span class="ft12b">Unassigned Tasks</span></div>
          <div id="CNT" style="display:none"></div></td>
        </tr>
      <?php if(sizeof($unassigned_tasks)>0){?>
      <tr>
        <td>
          <div id="UATL">
            <table width="100%" border="0" cellspacing="0" cellpadding="2">
              <?php foreach ($unassigned_tasks as $task){

			$bg = "#ffffff";

			if($row_count2 % 2){

				$bg = "#f0f0f0";

			}

			$row_count2++;

			$note_icon = "comments0.gif";

			if($note_list[$task['TaskID']]){

			if($note_list[$task['TaskID']]==1) $note_icon = "comments1.gif";

			if($note_list[$task['TaskID']]>1) $note_icon = "comments2.gif";

			}
		?>
              <tr bgcolor="<?php echo $bg;?>" onclick="window.location='task.php?ID=<?php echo $task['TaskID'];?>';" onmouseover="this.className='hilight'" onmouseout="this.className=''">
                <td width="16"><img src="images/priority<?php echo $task['set_priority'];?>.gif" /></td>
                <td width="16"><img src="images/status<?php echo $task['set_status'];?>.gif" /></td>
                <td width="16"><img src="images/<?php echo $note_icon;?>" /></td>
                <td align="right"><?php echo $task['set_complete'];?>%</td>
                <td nowrap="nowrap"><?php echo date("M jS, Y",strtotime($task['date_due']));?></td>
                <td width="100%" nowrap="nowrap"><div style="width:600px; overflow:hidden;"><?php echo $task['Subject'];?> <span style="color:#999"><?php echo str_chop(RemoveHTML($task['Body']),100);?></span></div></td>
                </tr>
              <?php } ?>
              </table>
            </div>        </td>
        </tr>
      <?php } else {?>
      <tr>
        <td style="padding:10px">No Tasks Found</td>
        </tr>
      <?php } ?>
      </table></td>
  </tr>
  <tr valign="bottom">
    <td height="36"><span class="ft12b">Other's Open Tasks:</span></td>
  </tr>
      <?php if(sizeof($assigned_tasks)>0){?>
      <tr>
        <td>
        <div id="UATL">
        <table width="100%" border="0" cellspacing="0" cellpadding="2">
        <?php foreach ($assigned_tasks as $task){

			$bg = "#ffffff";

			if($row_count2 % 2){

				$bg = "#f0f0f0";

			}

			$row_count2++;

			$note_icon = "comments0.gif";

			if($note_list[$task['TaskID']]){

			if($note_list[$task['TaskID']]==1) $note_icon = "comments1.gif";

			if($note_list[$task['TaskID']]>1) $note_icon = "comments2.gif";

			}
		?>
          <tr bgcolor="<?php echo $bg;?>" onclick="window.location='task.php?ID=<?php echo $task['TaskID'];?>';" onmouseover="this.className='hilight'" onmouseout="this.className=''">
            <td width="16"><img src="images/priority<?php echo $task['set_priority'];?>.gif" /></td>
            <td width="16"><img src="images/status<?php echo $task['set_status'];?>.gif" /></td>
            <td width="16"><img src="images/<?php echo $note_icon;?>" /></td>
            <td align="right"><?php echo $task['set_complete'];?>%</td>
            <td nowrap="nowrap"><?php echo date("M jS, Y",strtotime($task['date_due']));?></td>
            <td width="100%" nowrap="nowrap"><div style="width:500px; overflow:hidden;"><b><?php echo $user_list[$task['UserID']];?>&raquo;</b> <?php echo $task['Subject'];?> <span style="color:#999"><?php echo str_chop(RemoveHTML($task['Body']),100);?></span></div></td>
          </tr>
          <?php } ?>
        </table>
        </div>        </td>
      </tr>
      <?php } else {?>
      <tr>
        <td style="padding:10px">No Tasks Found</td>
      </tr>
      <?php } ?>
</table></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
</td>
  </tr>
</table>
<div id="dia_addtask" name="dia_addtask" title="Add a Task">
    <div id="dia_addtask_detail" name="dia_addtask_detail"></div>
</div>

</body>
</html>